﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using HU8.Web.FileExplorer.FileExplorer.Config;
using System.Configuration;

namespace HU8.Web.FileExplorer.FileExplorer
{
    public partial class Default : FileExplorerPage
    {
        private string rootPath;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = string.Format("{0} {1}", lang.Get("APP_TITLE"), config.Version); //load title
            
            ltrLanguage.Text = config.LanguageSelectBox();
            lbVersion.Text = config.Version;
            lbAboutTitle.Text = lang.Get("APP_TITLE");
            rootPath = "~/"; // config.BasePath;

            //set javascript paths
            hfCurrentPath.Value = GetRelativePath();
            //tbCurrentPath.Text = GetRelativePath().Replace("~/", "");
            litScript.Text = string.Format("var base_url = \"{0}\";", VirtualPathUtility.ToAbsolute(rootPath));
            litScript.Text += string.Format("var file_base_url = \"{0}\";", VirtualPathUtility.ToAbsolute(config.BasePath));
            litScript.Text += string.Format("var fe_url = \"{0}\";", VirtualPathUtility.ToAbsolute(config.FileExplorerPath));
        }

        public string GetRelativePath()
        {
            string relativePath = Request["Path"];
            if (string.IsNullOrEmpty(relativePath) || !relativePath.StartsWith(rootPath))
            {
                relativePath = rootPath;
            }
            return relativePath;
        }

        public string GetPath()
        {
            string relativePath = GetRelativePath();
            string path = Server.MapPath(relativePath);
            if (!Directory.Exists(path))
            {
                path = Server.MapPath(rootPath);
            }
            return path;
        }
    }
}